package ug.co.icom.kyazze.healthticket;

import ug.co.icom.kyazze.healthticket.data.MinUser;
import ug.co.icom.kyazze.healthticket.data.Permissions;
import ug.co.icom.kyazze.healthticket.data.RequestConnection;
import ug.co.icom.kyazze.healthticket.mongoHQ.MongoHQRequestConnection;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;

public class RequestConnectionActivity extends Activity implements OnClickListener {

	private MinUser user;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.requestconnection_layout);
		user = (MinUser) getIntent().getSerializableExtra("user");

		Button button = (Button) findViewById(R.id.button1);
		button.setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.button1)
		{
			try
			{
				Permissions permissions = new Permissions();
				permissions = getPermissions();
				RequestConnection rc = new RequestConnection();
				SharedPreferences settings = getSharedPreferences("ULS", 0);            	
				MinUser sendingUser = new MinUser();
				sendingUser.ID = settings.getString("uName", "");
				sendingUser.FirstName = settings.getString("fName", "");
				sendingUser.LastName = settings.getString("lName", "");
				sendingUser.OtherNames = settings.getString("oName", "");
				rc.ID = user.ID;
				rc.user = sendingUser;
				rc.permissions = permissions;
				MongoHQRequestConnection mrc = new MongoHQRequestConnection();
				Boolean ret = mrc.execute(rc).get();
				if(ret)
				{
					AlertDialog.Builder builder = new AlertDialog.Builder(RequestConnectionActivity.this);
					builder.setMessage("Connection Request Sent");
					builder.setCancelable(true);
					builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.cancel();	
							Intent i = new Intent(RequestConnectionActivity.this, MainActivity.class);
	                        startActivity(i);
						}
					});
					AlertDialog alert = builder.create();
					alert.show();
				}
				else
				{
					AlertDialog.Builder builder = new AlertDialog.Builder(RequestConnectionActivity.this);
					builder.setMessage("An Error Occured! Please Try Again");
					builder.setCancelable(true);
					builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.cancel();						
						}
					});
					AlertDialog alert = builder.create();
					alert.show();
				}
			}
			catch (Exception e) {
				AlertDialog.Builder builder = new AlertDialog.Builder(RequestConnectionActivity.this);
				builder.setMessage("An Error Occured! Please Try Again");
				builder.setCancelable(true);
				builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();						
					}
				});
				AlertDialog alert = builder.create();
				alert.show();
			}
		}

	}
	private Permissions getPermissions() {
		Permissions permissions = new Permissions();

		CheckBox phonenumber = (CheckBox) findViewById(R.id.checkBox1);
		CheckBox dateofbirth = (CheckBox) findViewById(R.id.checkBox2);
		CheckBox allergies = (CheckBox) findViewById(R.id.checkBox3);
		CheckBox medication = (CheckBox) findViewById(R.id.checkBox4);
		CheckBox immunization = (CheckBox) findViewById(R.id.checkBox5);
		CheckBox labdata = (CheckBox) findViewById(R.id.checkBox6);
		CheckBox conditions = (CheckBox) findViewById(R.id.checkBox7);
		CheckBox imaging = (CheckBox) findViewById(R.id.checkBox8);
		CheckBox insurance = (CheckBox) findViewById(R.id.checkBox9);
		CheckBox surgeries = (CheckBox) findViewById(R.id.checkBox10);
		
		
		permissions.PhoneNumber = (phonenumber.isChecked())?1:0;
		permissions.DateOfBirth = (dateofbirth.isChecked())?1:0;
		permissions.Allergies = (allergies.isChecked())?1:0;
		permissions.Medication = (medication.isChecked())?1:0;
		permissions.Immunization = (immunization.isChecked())?1:0;
		permissions.LabData = (labdata.isChecked())?1:0;
		permissions.Conditions = (conditions.isChecked())?1:0;
		permissions.Imaging = (imaging.isChecked())?1:0;
		permissions.Insurance = (insurance.isChecked())?1:0;
		permissions.Surgeries = (surgeries.isChecked())?1:0;

//		permissions.PhoneNumber = phonenumber.isChecked();
//		permissions.DateOfBirth = dateofbirth.isChecked();
//		permissions.Allergies = allergies.isChecked();
//		permissions.Medication = medication.isChecked();
//		permissions.Immunization = immunization.isChecked();
//		permissions.LabData = labdata.isChecked();
//		permissions.Conditions = conditions.isChecked();
//		permissions.Imaging = imaging.isChecked();
//		permissions.Insurance = insurance.isChecked();
//		permissions.Surgeries = surgeries.isChecked();

		return permissions;
	}
}
